/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.stepapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.me.androidresttest.WSCaller;

/**
 *
 * @author Amber
 */
public class Tasks extends Activity {
    public static final String MY_IPADDRESS = "128.237.242.174";
    private static final String NAMESPACE = "http://service.stepapp.me.org/";
    private static final String URL = "http://" + MY_IPADDRESS + ":8080/StepAppServer/StepAppService";
//    private static String SOAP_ACTION; // = "http://calc.me.org/add";
    private static String METHOD_NAME; // = "add";
    WSCaller caller = null;
    private String service_url;

    // GUI component declarations
    TextView tasks;

    TextView req_title;
    TextView del_title;
    TextView block_title;

    TextView req_phone;
    TextView del_phone;
    TextView block_phone;

    EditText req_et_phone;
    EditText del_et_phone;
    EditText block_et_phone;

    Button req_btn;
    Button del_btn;
    Button block_btn;

    private void initCaller() {
        if (this.caller == null) {
            this.caller = new WSCaller(NAMESPACE, URL);
        }
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.tasks);

        //  Add GUI components
        tasks = (TextView)findViewById(R.id.tasks);

        req_title = (TextView)findViewById(R.id.req_title);
        req_phone = (TextView)findViewById(R.id.req_phone);
        req_et_phone = (EditText)findViewById(R.id.req_et_phone);
        req_btn = (Button)findViewById(R.id.req_btn);
        
        del_title = (TextView)findViewById(R.id.del_title);
        del_phone = (TextView)findViewById(R.id.del_phone);
        del_et_phone = (EditText)findViewById(R.id.del_et_phone);
        del_btn = (Button)findViewById(R.id.req_btn);

        block_title = (TextView)findViewById(R.id.block_title);
        block_phone = (TextView)findViewById(R.id.block_phone);
        block_et_phone = (EditText)findViewById(R.id.block_et_phone);
        block_btn = (Button)findViewById(R.id.req_btn);
              

        // Login Button Listener
        req_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                requestFriend();
            }
        });

        // Login Button Listener
        del_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                deleteFriend();
            }
        });

        // Login Button Listener
        block_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                Toast.makeText(null, "Stalker Blocked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void requestFriend() {
        initCaller();
        TelephonyManager tMgr = (TelephonyManager) this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String mPhoneNumber = tMgr.getLine1Number();
        String requestNumber = req_et_phone.getText().toString();
        boolean hope = caller.requestFriendship(mPhoneNumber, requestNumber);
        //boolean found = caller.findByNumber(requestNumber);
        if (hope)  {
            Toast.makeText(this, "Friend Requested!", Toast.LENGTH_SHORT).show();
            }
           /* else {
                Toast.makeText(this, "Try again: Phone Number not in Database", Toast.LENGTH_SHORT).show();
            }
        }*/
        else {
           Toast.makeText(this, "SERVER ERROR!!!", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteFriend() {
        TelephonyManager tMgr = (TelephonyManager) this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String mPhoneNumber = tMgr.getLine1Number();
        String deleteNumber = del_et_phone.getText().toString();
        boolean hope = caller.deleteFriendship(mPhoneNumber, deleteNumber);
        if (hope) {
            Toast.makeText(this, "Friend Deleted!", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "SERVER ERROR", Toast.LENGTH_SHORT).show();
       }
    }
}
